package api

import (
	"context"
	"gdmin/servlet"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/route"
	"strconv"
)

func ResApi(r *route.RouterGroup) {
	r.POST("/res/button/save", createResButton)
	r.POST("/res/button/update", updateResButton)
	r.POST("/res/button/page", pageResButton)
	r.POST("/res/button/byId/:id", byIdResButton)
	r.POST("/res/button/delete/:id", deleteResButtonById)

	r.POST("/res/menu/save", createResMenu)
	r.POST("/res/menu/update", updateResMenu)
	r.POST("/res/menu/page", pageResMenu)
	r.POST("/res/menu/tree", treeResMenu)
	r.POST("/res/menu/byId/:id", byIdResMenu)
	r.POST("/res/menu/delete/:id", deleteResMenuById)

	r.POST("/res/api/save", createResApi)
	r.POST("/res/api/update", updateResApi)
	r.POST("/res/api/page", pageResApi)
	r.POST("/res/api/byId/:id", byIdResApi)
	r.POST("/res/api/list", listResApi)
	r.POST("/res/api/delete/:id", deleteResApiById)

	r.POST("/res/menu/bind_api", menuBindApi)

	return

}

// 接口列表
// @Tags      资源-接口
// @Summary   接口列表
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResApi}  "成功"
// @Router    /res/api/list [post]
func listResApi(ctx context.Context, c *app.RequestContext) {
	servlet.OkWithData(c, resService.ListApi())

	return
}

// 菜单树
// @Tags      资源-菜单
// @Summary   菜单树
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=model.NodeWithChildren}  "成功"
// @Router    /res/menu/tree [post]
func treeResMenu(ctx context.Context, c *app.RequestContext) {
	tree, err := resService.MenuTree(c.Query("id"))
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)

		return
	}
	servlet.OkWithData(c, tree)
	return
}

// 根据id删除接口
// @Tags      资源-接口
// @Summary   根据id删除接口
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=string}  "成功"
// @Router    /res/api/delete/:id [post]
func deleteResApiById(ctx context.Context, c *app.RequestContext) {
	resService.DeleteResApiById(c.Param("id"))
	servlet.Ok(c)
	return
}

// 根据id删除菜单
// @Tags      资源-菜单
// @Summary   根据id删除菜单
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=string}  "成功"
// @Router    /res/menu/delete/:id [post]
func deleteResMenuById(ctx context.Context, c *app.RequestContext) {
	resService.DeleteResMenuById(c.Param("id"))
	servlet.Ok(c)
	return
}

// 根据id删除按钮
// @Tags      资源-按钮
// @Summary   根据id删除按钮
// @Produce   application/json
// @Success   200   {object}  servlet.Resp{msg=string,data=string}  "成功"
// @Router    /res/button/delete/:id [post]
func deleteResButtonById(ctx context.Context, c *app.RequestContext) {
	resService.DeleteResButtonById(c.Param("id"))
	servlet.Ok(c)
	return

}

// 按钮分页
// @Tags      资源-按钮
// @Summary   按钮分页
// @accept    application/json
// @Produce   application/json
// @Param     data  body      servlet.QueryResButton             true  "查询参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.ResButtonResp}  "成功"
// @Router    /dept/update [post]
func pageResButton(ctx context.Context, c *app.RequestContext) {
	var req servlet.QueryResButton
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, resService.PageResButton(&req))

	return
}

// 按钮菜单
// @Tags      资源-菜单
// @Summary   菜单分页
// @accept    application/json
// @Produce   application/json
// @Param     data  body      servlet.QueryResMenu             true  "查询参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.ResMenuResp}  "成功"
// @Router    /dept/update [post]
func pageResMenu(ctx context.Context, c *app.RequestContext) {
	var req servlet.QueryResMenu
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, resService.PageResMenu(&req))

	return
}

// 按钮接口
// @Tags      资源-接口
// @Summary   接口分页
// @accept    application/json
// @Produce   application/json
// @Param     data  body      servlet.QueryResApi             true  "查询参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.ResApiResp}  "成功"
// @Router    /dept/update [post]
func pageResApi(ctx context.Context, c *app.RequestContext) {
	var req servlet.QueryResApi

	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, resService.PageResApi(&req))

	return

}

// 菜单绑定接口
// @Tags      资源-菜单
// @Summary   菜单绑定接口
// @Produce   application/json
// @Param     data  body      servlet.CreateMenuBindApi             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string}  "成功"
// @Router    /res/menu/bind_api [post]
func menuBindApi(ctx context.Context, c *app.RequestContext) {
	var req servlet.CreateMenuBindApi
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	err = resService.MenuBindApi(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.Ok(c)
	return
}

// 根据id查询接口
// @Tags      资源-接口
// @Summary   根据id查询接口
// @Produce   application/json
// @Param id path int true "id"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResApi}  "成功"
// @Router    /res/api/byId/:id [post]
func byIdResApi(ctx context.Context, c *app.RequestContext) {
	param := c.Param("id")
	num, err := strconv.Atoi(param)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	} else {
		servlet.OkWithData(c, resService.QueryApiById(num))
		return
	}
}

// 修改接口
// @Tags      资源-接口
// @Summary   修改接口
// @Produce   application/json
// @Param     data  body      servlet.UpdateResApiReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResApi}  "成功"
// @Router    /res/api/update [post]
func updateResApi(ctx context.Context, c *app.RequestContext) {
	var req servlet.UpdateResApiReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	api, err := resService.UpdateApi(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, api)
	return
}

// 创建接口
// @Tags      资源-接口
// @Summary   创建接口
// @Produce   application/json
// @Param     data  body      servlet.CreateResApiReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResApi}  "成功"
// @Router    /res/api/save [post]
func createResApi(ctx context.Context, c *app.RequestContext) {
	var req servlet.CreateResApiReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	api, err := resService.CreateApi(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, api)
	return
}

// 根据id查询菜单
// @Tags      资源-菜单
// @Summary   根据id查询菜单
// @Produce   application/json
// @Param id path int true "id"
// @Success   200   {object}  servlet.Resp{msg=string,data=servlet.ResMenuResp}  "成功"
// @Router    /res/menu/byId/:id [post]
func byIdResMenu(ctx context.Context, c *app.RequestContext) {
	param := c.Param("id")
	num, err := strconv.Atoi(param)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	} else {
		servlet.OkWithData(c, resService.QueryMenuById(num))
		return
	}
}

// 修改菜单
// @Tags      资源-菜单
// @Summary   修改菜单
// @Produce   application/json
// @Param     data  body      servlet.UpdateResMenuReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResMenu}  "成功"
// @Router    /res/menu/update [post]
func updateResMenu(ctx context.Context, c *app.RequestContext) {
	var req servlet.UpdateResMenuReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	menu, err := resService.UpdateMenu(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, menu)
	return
}

// 创建菜单
// @Tags      资源-菜单
// @Summary   创建菜单
// @Produce   application/json
// @Param     data  body      servlet.CreateResMenuReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResMenu}  "成功"
// @Router    /res/menu/save [post]
func createResMenu(ctx context.Context, c *app.RequestContext) {

	var req servlet.CreateResMenuReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	menu, err := resService.CreateMenu(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, menu)
	return

}

// 根据id查询按钮
// @Tags      资源-按钮
// @Summary   根据id查询按钮
// @Produce   application/json
// @Param id path int true "id"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResButton}  "成功"
// @Router    /res/button/byId/:id [post]
func byIdResButton(ctx context.Context, c *app.RequestContext) {
	param := c.Param("id")
	num, err := strconv.Atoi(param)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	} else {
		servlet.OkWithData(c, resService.QueryButtonById(num))
		return
	}
}

// 修改按钮
// @Tags      资源-按钮
// @Summary   修改按钮
// @Produce   application/json
// @Param     data  body      servlet.UpdateResButtonReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResButton}  "成功"
// @Router    /res/button/update [post]
func updateResButton(ctx context.Context, c *app.RequestContext) {
	var req servlet.UpdateResButtonReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	menu, err := resService.UpdateButton(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, menu)
	return
}

// 创建按钮
// @Tags      资源-按钮
// @Summary   创建按钮
// @Produce   application/json
// @Param     data  body      servlet.CreateResButtonReq             true  "参数"
// @Success   200   {object}  servlet.Resp{msg=string,data=model.ResButton}  "成功"
// @Router    /res/button/save [post]
func createResButton(ctx context.Context, c *app.RequestContext) {
	var req servlet.CreateResButtonReq
	err := c.Bind(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}

	role, err := resService.CreateButton(&req)
	if err != nil {
		servlet.FailWithMessage(err.Error(), c)
		return
	}
	servlet.OkWithData(c, role)
	return
}
